//
//  String+Extension.swift
//  EastNetWorkProject
//
//  Created by hlt on 2023/10/27.
//

import Foundation
import UIKit
import CryptoKit
extension String{
    //对string的字符都进行编码
    func escape(string:String) -> String {
        let allowedCharacters =  CharacterSet(charactersIn:string).inverted
        return self.addingPercentEncoding(withAllowedCharacters: allowedCharacters) ?? ""
    }
    
    func stringFormat(fromFormatter:String,toFormatter:String) -> String{
        let format = DateFormatter()
        format.dateFormat = fromFormatter
        format.timeZone = NSTimeZone.local
        if let date = format.date(from: self){
            let format1 = DateFormatter()
            format1.dateFormat = fromFormatter
            format1.timeZone = NSTimeZone.local
            let dateString = format1.string(from: date)
            return dateString
        }
        return ""
    }
    
}

extension String{
    func getWidth(_ height:CGFloat,attributes:[NSAttributedString.Key : Any]? = nil) -> CGFloat{
        let str = self as NSString
        let rect = str.boundingRect(with: CGSize(width: CGFLOAT_MAX, height: height), options: [.usesLineFragmentOrigin,.usesFontLeading], attributes: attributes, context: nil)
        return ceil(rect.size.width)
    }
    
    func getHeight(_ weight:CGFloat,attributes:[NSAttributedString.Key : Any]? = nil) -> CGFloat{
        let str = self as NSString
        let rect = str.boundingRect(with: CGSize(width:weight, height:CGFLOAT_MAX), options: [.usesLineFragmentOrigin,.usesFontLeading], attributes:attributes, context: nil)
        return ceil(rect.size.height)
    }
}

extension String {
    //MARK: MD5加密
    public func md5() -> String {
        let digest = Insecure.MD5.hash(data:self.data(using: .utf8)!)
        return digest.map {
                   String(format: "%02hhx", $0)
               }.joined()
    }
    
    var SHA1String:String{
        let data = self.data(using: .utf8)!
        let digest = Insecure.SHA1.hash(data: data)
        let hash = digest.map { String(format: "%02hhx", $0) }.joined()
        return hash
    }
    
    
    var SHA256String:String{
        let data = self.data(using: .utf8)!
        let digest = SHA256.hash(data: data)
        let hash = digest.map { String(format: "%02hhx", $0) }.joined()
        return hash
    }
    
    var SHA384String:String{
        let data = self.data(using: .utf8)!
        let digest = SHA384.hash(data: data)
        let hash = digest.map { String(format: "%02hhx", $0) }.joined()
        return hash
    }
    
    var SHA512String:String{
        let data = self.data(using: .utf8)!
        let digest = SHA512.hash(data: data)
        let hash = digest.map { String(format: "%02hhx", $0) }.joined()
        return hash
    }

    func base64EncodeString() -> String{
        
        return self.data(using: .utf8)!.base64EncodedString()
    }
    
    
}

extension String{
    public func urlEncode() -> String {
        let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
        let subDelimitersToEncode = "!$&'()*+,;="
        let encodableDelimiters = CharacterSet(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
        let encodeCharacterSet =  CharacterSet.urlQueryAllowed.subtracting(encodableDelimiters)
        let encodeString = self.addingPercentEncoding(withAllowedCharacters: encodeCharacterSet)
        return encodeString ?? self
    }
    
    //指定字符+汉字编码
    func stringPercentEncoding(characters:String) -> String{
        let encodableDelimiters = CharacterSet(charactersIn:characters).inverted
        let newStr = self.addingPercentEncoding(withAllowedCharacters: encodableDelimiters)
        return newStr ?? self
    }
}




extension String{
    var isBlank:Bool{
        return self.trimmingCharacters(in: .whitespaces).isEmpty
    }
}
